import Base from 'models/base';

import moment from 'moment';

export default class GoodsActivityForm extends Base {
  branchId = '';
  partnerId = '';
  goodsDefId = '';
  activityTypeId = '';
  activityStartTime = null;
  activityEndTime = null;
  status = 1;

  constructor(activity) {
    super();
    if (activity === undefined) {
      return;
    }

    let fields = ['branchName', 'partnerName', 'goodsDefId', 'goodsName', 'activityTypeId', 'activityTypeName'];
    this.initObject(activity, fields);

    let startTime = activity.activityStartTime;
    if (startTime !== null) {
      startTime = moment(startTime).valueOf();
    }
    this.activityStartTime = startTime;

    let endTime = activity.activityEndTime;
    if (endTime !== null) {
      endTime = moment(endTime).valueOf();
    }
    this.activityEndTime = endTime;

    let status = activity.status;
    this.status = Number(status);

    this.id = activity.activityGoodsId;
  }

  checkValid() {
    let fields = ['activityTypeId', 'branchId', 'goodsDefId', 'activityStartTime', 'activityEndTime'];
    let code = this.checkRequired(fields);
    if (code !== '') {
      return code;
    }

    let startTime = this.activityStartTime;
    let endTime = this.activityEndTime;
    if (moment(startTime).isAfter(endTime, 'second')) {
      return 'startTime>endTime';
    }
    return '';
  }

  getObject() {
    let postData = {};
    let fields = ['activityTypeId', 'goodsDefId', 'status'];
    this.setPostData(postData, fields);

    let startTime = this.activityStartTime;
    postData.activityStartTime = moment(startTime).format('YYYY-MM-DD HH:mm:ss');
    let endTime = this.activityEndTime;
    postData.activityEndTime = moment(endTime).format('YYYY-MM-DD HH:mm:ss');

    let id = this.id;
    if (id !== undefined) {
      postData.activityGoodsId = id;
    }

    return postData;
  }
}
